﻿using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;

namespace XFControls.XFPanels
{
    public enum XFPanelHeaderState
    {
        Enabled,
        Disabled
    }

    public partial class XFPanelHeader : XFPanelBase
    {
        private XFPanelHeaderState _curState;

        public XFPanelHeader()
        {
            CenterTextColor = Color.White;
            InitializeComponent();
            BackgroundColor = Color.Black;
        }

        public XFPanelContainer MainPanelContainer { get; set; }
        public Color BackgroundColor { get; set; }
        public string CenterText { get; set; }
        public Color CenterTextColor { get; set; }
        public IXFButton LeftButton { get; set; }
        public IXFButton RightButton { get; set; }

        public void DisableHeader(bool disable)
        {
            if (disable)
            {
                _curState = XFPanelHeaderState.Disabled;
            }
            else
            {
                _curState = XFPanelHeaderState.Enabled;
            }
        }

        public override void PanelPaint(Graphics g)
        {
            using (var b = new SolidBrush(BackgroundColor))
                g.FillRectangle(b, 0, 0, Width, Height);

            DrawUI("header", g, new Rectangle(0, 0, Width, Height));

            if (string.IsNullOrEmpty(CenterText))
                return;

            int xmid = Width >> 1;
            int ymid = Height >> 1;

            using (var f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold))
            {
                string disp = CenterText;
                if (CenterText.Length > 20)
                    disp = disp.Substring(0, 20) + "...";

                xmid -= (int)(g.MeasureString(disp, f).Width) >> 1;
                ymid -= (int)(g.MeasureString(disp, f).Height) >> 1;

                using (var b = new SolidBrush(CenterTextColor))
                    g.DrawString(disp, f, b, xmid, ymid);
            }

            if (LeftButton != null)
                LeftButton.ButtonPaint(g, 5, 5);
            if (RightButton != null)
                RightButton.ButtonPaint(g, Width - 5 - RightButton.Width, 5);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (_curState == XFPanelHeaderState.Disabled)
                return;

            if (LeftButton != null && e.X < LeftButton.Width + 5)
            {
                LeftButton.Click();
            }

            if (RightButton != null && e.X > Width - 5 - RightButton.Width)
            {
                RightButton.Click();
            }
        }

        public void DrawUI(string uri, Graphics g, Rectangle rec)
        {
            DrawUI(uri, g, rec, true);
        }

        public void DrawUI(string uri, Graphics g, Rectangle rec, bool maintainImgRatio)
        {
            byte[] buffer = null;


            if (XFGraphics.ImageCache.ContainsKey(uri))
            {
                var val = XFGraphics.ImageCache[uri];
                if (File.Exists(val))
                {
                    var reader = File.OpenRead(XFGraphics.ImageCache[uri]);
                    buffer = new byte[reader.Length];
                    reader.Read(buffer, 0, buffer.Length);
                }
            }
            if (buffer == null)
            {
                var myAssembly = GetType().Assembly;
                string ns = myAssembly.GetName().Name;
                var ms = (MemoryStream)myAssembly.GetManifestResourceStream(ns + ".Resources." + uri + ".png");
                if (ms == null)
                {
                    myAssembly = Assembly.GetExecutingAssembly();
                    ns = myAssembly.GetName().Name;
                    ms = (MemoryStream)myAssembly.GetManifestResourceStream(ns + ".Resources." + uri + ".png");
                    if (ms == null) return;
                }

                buffer = ms.GetBuffer();

                if (buffer == null) return;
            }

            XFGraphics.DrawMemoryStream(g, rec, buffer, maintainImgRatio);
        }


    }
}